using System.Collections.Generic;

#if UNITY_EDITOR
[System.Serializable]
#endif
public partial class ConstData : ISerialize
{
    #region Instance

    static ConstData _instance;
    public static ConstData Instance => _instance;
    public static void SetInstance(ConstData instance)
    {
        if (instance == null)
        {
            GameDebug.LogError("instance null");
            return;
        }

        _instance = instance;
    }

    #endregion

    public uint GetCRC()
    {
        return CRC32.GetCRC32(this.Write());
    }
    
#if UNITY_EDITOR
    [UnityEngine.SerializeField]
#endif
    private GoodsArche[] m_Goods;

    public GoodsArche[] goods
    {
        get { return m_Goods; }
        set { m_Goods = value; }
    }

    private AINameArche[] m_AINameArches;

    public AINameArche[] aiNameArche
    {
        get => m_AINameArches;
        set => m_AINameArches = value;
    }

    private TipsArche[] m_Tips;
    public TipsArche[] tips
    {
        get => m_Tips;
        set => m_Tips = value;
    }

    private AdConfig m_AdConfig;
    public AdConfig adConfig
    {
        get => m_AdConfig;
        set => m_AdConfig = value;
    }
    
    private LanguageArche[] m_Langs;
    public LanguageArche[] langs
    {
        get => m_Langs;
        set => m_Langs = value;
    }

    public static T FindWithId<T>(T[] array, int id,string tableName)where T:ISerialize
    {
        if(array == null)
        {
            GameDebug.LogError("FindWithId  array == null");
            return null;
        }

        int index = id - 1;

        if(index <0 || index >= array.Length)
        {
            GameDebug.LogError($"{tableName} invalid id:{id}");
            return null;
        }
        
        var o = array[index];

        return o;
    }

    public GoodsArche GetGoods(string goodsId)
    {
        GoodsArche goods = System.Array.Find(m_Goods, e => e.goodsId == goodsId);

        if(goods == null)
            GameDebug.LogError($"cannot find goods:{goodsId}");

        return goods;
    }

    public string GetRandomAIName()
    {
        if (m_AINameArches.Length == 0)
        {
            GameDebug.LogError($"m_AINameArche == null:{m_AINameArches == null}");
            return "Mike";
        }
        int randIndex = CommonFunctions.RandomRange(0,this.aiNameArche.Length);
        return m_AINameArches[randIndex].name;
    }

    public string GetTips(int index)
    {        
        if (m_Tips.Length == 0)
        {
            GameDebug.LogError($"m_Tips.Length == 0");
            return "";
        }
        
        index %= m_Tips.Length;
        
        return m_Tips[index].content;
    }
    
    
    public int GetLoginBonusDiamond(int day)
    {
        LoginBonusDiamond.TryGetValue(day,out var diamond);
        return diamond;
    }
    
    public static Dictionary<int, int> LoginBonusDiamond = new Dictionary<int, int>
    {
        { 1, 0},
        { 2, 10},
        { 3, 15},
        { 4, 20},
        { 5, 25},
        { 6, 30},
        { 7, 35},
        { 8, 40},
        { 9, 45},
        { 10, 50},
        { 11, 55},
        { 12, 60},
        { 13, 65},
        { 14, 70},
        { 15, 75},
        { 16, 80},
        { 17, 85},
        { 18, 90},
        { 19, 95},
        { 20, 100},
        { 21, 105},
        { 22, 110},
        { 23, 115},
        { 24, 120},
        { 25, 125},
        { 26, 130},
        { 27, 135},
        { 28, 140},
        { 29, 145},
        { 30, 150},
    };
    
    
}